import 'dart:io';
import 'package:kmxy_base/kmxy_base.dart';
import 'package:kmxy_base/network/net.dart';
import 'package:kmxy_base/util/event.dart';
import 'package:kmxy_base/util/file_util.dart';
import 'package:kmxy_base/util/sp_util.dart';
import 'package:package_info_plus/package_info_plus.dart';

class ApkUtil {
  static Map updateData = {};

  static String SP_IS_DOWN_APK = 'SP_IS_DOWN_APK';
  static int EVENT_DOWNLOAD_APK_PROGRESS = 4000;

  /// 检查是否有新版本
  static Future<bool> getCheckVersion(String appid) async {
    if (!Platform.isAndroid) {
      return false;
    }
    //获取当前版本
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    packageInfo.version;

    var respone = await Net.tempDio.get(
      'http://oa.xykjcloud.com/api/projects/appserver/info?appid=$appid',
    );
    if (respone.data['code'] != 200) {
      return false;
    }
    updateData = respone.data['data'];
    if (!await KmxyBase.isArm64V8a()) {
      updateData['url'] =
          updateData['url']?.replaceAll('arm64-v8a', 'armeabi-v7a');
    }
    String versionN = respone.data['data']['version'] ?? '';
    if (!versionN.contains('+')) {
      return false;
    }
    updateData['versionName'] = versionN.split('+')[0];
    updateData['versionCode'] = versionN.split('+')[1];
    int versionIntL = int.parse(packageInfo.version.replaceAll('.', ''));
    int versionIntN = int.parse(versionN.split('+')[0].replaceAll('.', ''));
    if (versionIntL >= versionIntN) {
      String fileDir = await FileUtil.getCacheDir();
      String fileName = FileUtil.getFileName(updateData['url']);
      File installFile = File('$fileDir/install_$fileName');
      bool exists = await installFile.exists();
      if (exists) {
        await installFile.delete();
      }
      await SpUtil.setBool(SP_IS_DOWN_APK, false);
      return false;
    }
    return true;
  }

  // 更新app
  static Future<void> upDateVersion() async {
    String fileDir = await FileUtil.getCacheDir();
    String fileName = FileUtil.getFileName(updateData['url']);
    File file = File('$fileDir/$fileName');
    File installFile = File('$fileDir/install_$fileName');
    bool isDirect = await isDirectUpdate();
    if (isDirect) {
      KmxyBase.installApk(installFile.path);
    } else {
      Net.downloadTemp(updateData['url'], file.path, progress: (int progress) {
        Event.eventBus.fire(Event(EVENT_DOWNLOAD_APK_PROGRESS, progress));
      }, finish: (String savePath) async {
        await File(savePath).copy(installFile.path);
        await file.delete();
        await SpUtil.setBool(SP_IS_DOWN_APK, true);
        KmxyBase.installApk(installFile.path);
      }, error: (e) {});
    }
  }

  // 是否可以直接更新更新app
  static Future<bool> isDirectUpdate() async {
    String fileDir = await FileUtil.getCacheDir();
    String fileName = FileUtil.getFileName(updateData['url']);
    File installFile = File('$fileDir/install_$fileName');
    bool isDownApk = await SpUtil.getBool(SP_IS_DOWN_APK) ?? false;
    bool exists = await installFile.exists();
    bool isNetApk = true;
    if (exists) {
      String apkVersionName =
          await KmxyBase.getApkVersionName(installFile.path);
      if (updateData['versionName'] != apkVersionName) {
        isNetApk = false;
      }
    }
    return exists && isDownApk && isNetApk;
  }
}
